<template>
  <SysDialog
    draggable
    :title="dialog.title"
    :marginTop="dialog.marginTop"
    :visible="dialog.visible"
    :width="dialog.width"
    :height="dialog.height"
    @onClose="onClose"
    @onConfirm="confirm"
  >
    <template v-slot:content>
      <!-- 内容组件 -->
      <el-form
        :model="dialogModel"
        ref="addExperienceForm"
        :rules="rules"
        label-width="120px"
        size="default"
      >
        <el-form-item prop="experienceName" label="经验名称">
          <el-input v-model="dialogModel.experienceName" clearable></el-input>
        </el-form-item>
      </el-form>
      <wandEditor @changeText="changeText" ref="RefWangEditor"></wandEditor>
      <!-- 文件上传 -->
      <fileUpMore ref="RefFileUpMore" @fileUp="fileUp" style="margin-top: 10px;"></fileUpMore>
      <div v-if="dialogModel.id && files.length">
        <h4 style="color: #fff;">历史文件</h4>
        <div v-for="item in files" :key="item">
          <div class="historyFileOne">
            <span style="color: #00ffd2"
              >{{ item.fileName }}
              <el-button size="small" @click="historyDelFile(item)"
                >删除</el-button
              ></span
            >
          </div>
        </div>
      </div>
    </template>
  </SysDialog>
</template>

<script setup lang='ts'>
import { ref, nextTick } from "vue";
import { ElForm } from "element-plus";
// import SysDialog from "@/components/SysDialog.vue";
import useDialog from "@/hooks/useDialog";
import useInstance from "@/hooks/useInstance";
import { EditType, Title } from "@/type/BaseEnum";
import addEditModel from "../tsModel/AddEdit";
import { ExperienceDelFilesApi } from "@/api/Experience/ExperienceList";
import { wandEditorDate } from "@/utils/index";
// 使用
const addExperienceForm = ref<InstanceType<typeof ElForm>>();
const RefFileUpMore = ref();
const { dialog, onShow } = useDialog();
const { global, proxy } = useInstance();
const { dialogModel, rules } = addEditModel();
const RefWangEditor = ref();
const files = ref<any>([]);
const onClose = () => {
  // 清楚数据 第一个是wangeditor 第二个文件上传
  RefFileUpMore.value?.clearFile();
  dialog.visible = false;
};
const historyDelFile = async (item) => {
  console.log(item);
  const confirm = await global.$myconfirm("确定删除该文件嘛?");
  if (confirm) {
    const res = await ExperienceDelFilesApi({ fileId: item.id });
    if (res.code === 200) {
      global.$message({ message: "删除成功", type: "success" });
      files.value.splice(item,1)
      console.log(files.value);
    } else {
      global.$message({ message: "删除失败", type: "error" });
    }
  } else {
    global.$message({ message: "已取消删除", type: "warning" });
  }
};
const fileUp = (row) => {
  console.log(row);
  // 赋值数据
  dialogModel.wordFiles = row;
};
// 父级数据
// const props = defineProps({

// })
// 保存
const emit = defineEmits(["save"]);
const changeText = (row) => {
  dialogModel.description = row;
};
const confirm = () => {
  // confirm =
  console.log("cccccc");

  addExperienceForm.value?.validate((valid) => {
    //验证通过，才提交表单
    if (valid) {
      // 这里 提交验证
      RefWangEditor.value.delImages(); // 调用删除图片的方法
      emit("save", dialogModel);
      onClose();
    }
  });
};
const show = (type: string, row?: any) => {
  console.log("show");
  dialog.width = 1050;
  dialog.height = 700;
  dialog.marginTop = "5vh";
  dialog.title = type === EditType.ADD ? Title.ADD : Title.EDIT;
  onShow();
  // 富文本数据回显 新增 编辑
  global.$resetForm(addExperienceForm.value, dialogModel);
  if (type === EditType.EDIT) {
    global.$objCopy(row, dialogModel);
    nextTick(async () => {
      files.value = row.files;
      const newSt = await wandEditorDate(dialogModel.description);
      RefWangEditor.value.valueHtml = newSt;
    });
  } else {
    nextTick(() => {
      RefWangEditor.value.valueHtml = dialogModel.description;
    });
  }
};
defineExpose({
  show
});
</script>
<style scoped lang='scss'>
.historyFileOne {
  height: 30px;
}
</style>